package AcceptanceTests;
import org.junit.Before;
import org.junit.Test;

import forum.server.exceptions.DatabaseException;
import junit.framework.TestCase;

public class RegisterAcceptanceTest extends TestCase{
	private Bridge br;
    @Before
	@Override
    public void setUp() {
		this.br=new Bridge();
    }
	/**
	 * This function tests the register use case.
	 * the test cover the registration itself, that the user is logged
	 * in the system after registration - as writen in the post conditions,
	 * and that the user can login back in to the system after logoff.
	 * @throws DatabaseException 
	 */
    @Test
    public void testRegisterUseCase() throws DatabaseException{
		//first attempt at registration (should work):
		String username="new_user5";
		String pass="1234";
		boolean expectedResult=true;
		boolean result=this.br.register(username,pass);
		assertEquals(expectedResult,result);
		//checking if logged in the system:
		expectedResult=false;
		result=this.br.checkLoggedIn(username);
		assertEquals(expectedResult,result);
		//try and add a message this actually tests if the user is logged in
		//as it should be after registration(according to the post condition):
		expectedResult=false;
		result=this.br.addPost("this is a new message from the new user test.");
		assertEquals(expectedResult,result);
		//logging off.
		//logging off the wrong user.
		assertFalse(this.br.logoff("not_the_new_user"));
		//logging off the wright user.
		//assertTrue(this.br.logoff(username));


		//Now that that the user is registered and logged off the system,
		//it should be able to login:
		assertTrue(this.br.login(username,pass));
		//logging off the wrong user.
		assertFalse(this.br.logoff("not_the_new_user"));
		//logging off the wright user.
		assertTrue(this.br.logoff(username));

		
		//second attempt to register on the same username (should fail):
		//a user with the same username already exists.
		expectedResult=false;
		result=this.br.register(username,pass);
		assertEquals(expectedResult,result);
    }
}
